/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.jit.core.common;

import io.iec.edp.caf.common.environment.EnvironmentUtil;
import io.iec.edp.caf.commons.runtime.CafEnvironment;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
public class DirectoryManager {
    private static final String JIT_DIR = "jit";
    private static final String JIT_BASE_DIR;
    private static String HOME_DIR = null;
    private static final Object lockObj = new Object();

    static {
        String deployUnitName = CafEnvironment.getCurrentDeployunitName();
        String jitBaseDir = "var" + File.separator;
        if (StringUtils.isBlank(deployUnitName)) {
            JIT_BASE_DIR = jitBaseDir + JIT_DIR;
        } else {
            // 同一份安装盘会同时启动多个进程来加载多个程序，通过deployUnitName区分不同进程，保证每个进程读写自己的文件
            JIT_BASE_DIR = jitBaseDir + deployUnitName + File.separator + JIT_DIR;
        }
    }

    public static String getHomeDir() {
        if (HOME_DIR == null) {
            HOME_DIR = EnvironmentUtil.getServerRTPath();
        }
        return HOME_DIR;
    }

    public static String getJitBaseDirectory() {
        return getHomeDir() + File.separator + JIT_BASE_DIR;
    }

    public static String getJitDirectory(String functionType) {
        return getJitBaseDirectory() + File.separator + functionType;
    }

    public static String getJitDirectory(String functionType, String functionId) {
        return getJitDirectory(functionType) + File.separator + functionId;
    }

    public static String get3rdDirectory() {
        return getHomeDir() + File.separator + "runtime" + File.separator + "3rd";
    }

    /**
     * 获取指定目录的所有直接子文件夹的名称列表。
     *
     * @param parentDirPath 父文件夹路径
     * @return 所有直接子文件夹的名称列表，只包含文件名，不包含路径信息。
     */
    public static List<String> listChildrenDirName(String parentDirPath) {
        File parentDir = new File(parentDirPath);
        if (!parentDir.exists()) {
            return Collections.emptyList();
        }

        if (parentDir.isFile()) {
            return Collections.emptyList();
        }

        File[] children = null;
        try {
            children = parentDir.listFiles();
        } catch (SecurityException e) {
            log.error("获取文件夹信息异常：" + e.getMessage(), e);
        }

        if (children == null || children.length == 0) {
            return Collections.emptyList();
        }

        List<String> result = new ArrayList<>();
        for (File child : children) {
            if (child.isDirectory()) {
                result.add(child.getName());
            }
        }
        return result;
    }

    public static boolean createDir(String creatingDir) {
        File file = new File(creatingDir);
        if (!file.exists()) {
            synchronized (lockObj) {
                if (!file.exists()) {
                    return file.mkdirs();
                }
            }
        }
        return false;
    }

    public static String joinFilePath(String... paths) {
        return StringUtils.join(paths, File.separator);
    }
}
